﻿package com._public._control 
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author WZH(shch8.com)
	 * 本类用于处理使用鼠标调整显示对象
	 * 
	 * var sprAdj:SprAdjust=new SprAdjust(editBar, stage, adjustHeight);
		//SprAdjust(要调整的感应元件,_stage舞台对象,adjust调整时的反回函数)
		sprAdj.adjustObj = adjustObj;/调整时返回函数
		sprAdj.upFun = upFun;//弹起返回函数
		sprAdj.outFun = outFun;/移入返回函数
		sprAdj.overFun = overFun;//移出返回函数
		sprAdj.downFun = downFun;/按下返回函数
		
		当_clickFun有值时，如果鼠标点下弹起没有移动则回调_clickFun，若点下弹起有移动则回调_upFun
	 */
	public class SprAdjust 
	{
		private var _adjustFun:Function;
		private var _downFun:Function;
		private var _overFun:Function;
		private var _outFun:Function;
		private var _upFun:Function;
		private var _clickFun:Function;
		private var _adjustObj:DisplayObject;
		private var stage:Stage;
		public var atMove:Boolean;//判断是否有移动过
		/**
		 * 
		 * @param	adjustObj	要调整的感应元件
		 * @param	_stage		舞台对象
		 * @param	adjust		调整时的反回函数
		 */
		public function SprAdjust(adjustObj:DisplayObject,_stage:Stage,adjust:Function=null) 
		{
			_adjustObj = adjustObj;
			_adjustFun = adjust;
			stage = _stage;
			_adjustObj.addEventListener(MouseEvent.MOUSE_OVER, handler);
			_adjustObj.addEventListener(MouseEvent.MOUSE_DOWN, handler);
		}
		public function set adjustFun(value:Function):void
		{
			_adjustFun = value;
		}
		public function set downFun(value:Function):void
		{
			_downFun = value;
		}
		public function set clickFun(value:Function):void
		{
			_clickFun = value;
		}
		public function set upFun(value:Function):void
		{
			_upFun = value;
		}
		public function set overFun(value:Function):void
		{
			_overFun = value;
		}
		public function set outFun(value:Function):void
		{
			_outFun = value;
		}
		/*清除调整*/
		public function clear():void
		{
			_adjustObj.removeEventListener(MouseEvent.MOUSE_OVER, handler);
			_adjustObj.removeEventListener(MouseEvent.MOUSE_DOWN, handler);
		}
		/*停止调整控制*/
		public function stop():void
		{
			_adjustObj.removeEventListener(MouseEvent.MOUSE_OUT, handler);
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, handler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, handler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, handler);
			_adjustObj.addEventListener(MouseEvent.MOUSE_OVER, handler);
			_adjustObj.addEventListener(MouseEvent.MOUSE_DOWN, handler);
		}
		private function handler(event:MouseEvent):void {
			if (event == null) return;
            switch(event.type) {  
				case MouseEvent.MOUSE_OVER:
					_adjustObj.addEventListener(MouseEvent.MOUSE_OUT, handler)
					stage.addEventListener(MouseEvent.MOUSE_DOWN, handler);
					_adjustObj.removeEventListener(MouseEvent.MOUSE_OVER, handler)
					if (_overFun is Function)_overFun(event);
				break;
				case MouseEvent.MOUSE_OUT:
					_adjustObj.removeEventListener(MouseEvent.MOUSE_OUT, handler);
					stage.removeEventListener(MouseEvent.MOUSE_DOWN, handler);
					_adjustObj.addEventListener(MouseEvent.MOUSE_OVER, handler);
					_adjustObj.addEventListener(MouseEvent.MOUSE_DOWN, handler);
					if (_outFun is Function)_outFun(event);
				break;
				case MouseEvent.MOUSE_MOVE:
					if (_adjustFun is Function)_adjustFun(event);
					atMove = true;
				break;
				case MouseEvent.MOUSE_DOWN:
					atMove = false;
					_adjustObj.removeEventListener(MouseEvent.MOUSE_OUT, handler);
					stage.removeEventListener(MouseEvent.MOUSE_DOWN, handler);
					stage.addEventListener(MouseEvent.MOUSE_UP, handler);
					stage.addEventListener(MouseEvent.MOUSE_MOVE, handler);
					if (_downFun is Function)_downFun(event);
				break
				case MouseEvent.MOUSE_UP:
					stage.removeEventListener(MouseEvent.MOUSE_UP, handler);
					stage.removeEventListener(MouseEvent.MOUSE_MOVE, handler);
					_adjustObj.addEventListener(MouseEvent.MOUSE_OVER, handler);
					_adjustObj.addEventListener(MouseEvent.MOUSE_DOWN, handler);
					if((_clickFun is Function)&&(_upFun is Function)){
					if(atMove){
						_upFun(event);
					}else {
						_clickFun(event);
						}
					}else {
						if (_clickFun is Function)_clickFun(event);
						if (_upFun is Function)_upFun(event);
						}
				break
			}
		}
	}
	
}